using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public abstract class LightRayHandler : Clickable {
	
	
	public Color currentColor;
	public bool isFixed = false;
	
	private bool correctPosition = false;
	
	private int id;
	private ObjectType type = ObjectType.Unknown;
	
	public abstract List<LightHitInfo> getNextLightRay(LightHitInfo hitInfo);
	
	public override void onClick(){
		
	}
	
	public virtual void initialize(int newId, Vector3 position, float rotation, Color c, bool fixedObj){
		Id = newId;
		transform.position = position;
		transform.Rotate(new Vector3(0,0,rotation));
		isFixed = fixedObj;
		currentColor = c;
	}
	
	public virtual void FixedUpdate(){
		if(particleSystem != null){
			if(THistory.Instance.isUndoing()){
				if(correctPosition){
					particleSystem.startColor = Color.green;
				}else{
					if(GhostsHandler.Instance.shouldBeMoved(id)){
						particleSystem.startColor = new Color(1f,0.5f,0f,1f);	
					}else{
						particleSystem.startColor = Color.red;
					}
				}
				if(!particleSystem.isPlaying){
					particleSystem.Play();
				}
			}else{
				particleSystem.Stop();
			}
		}
		
		correctPosition = false;
	}
	
	public virtual void OnTriggerStay(Collider other){
		if(other.GetComponent<GhostCollideHandler>() != null){
			if(other.GetComponent<GhostCollideHandler>().type == Type){
				correctPosition = checkCorrectPosition(other.transform);
			}
		}
	}
	
	public virtual bool checkCorrectPosition(Transform ghost){
		
		if(Vector3.Distance(ghost.position,transform.position) < 0.3f ){
			return Mathf.Abs(transform.eulerAngles.z%90-ghost.eulerAngles.z%90) < 4.0f;
		}
		return false;
	}
	
	public ObjectType Type{
		get
		{
			return this.type;
		}
		set{
			type = value;
		}
		
	}
	
	public bool CorrectPosition{
		get
		{
			return this.correctPosition;
		}
		set{
			correctPosition = value;
		}
		
	}
	
	public int Id{
		get{
			return this.id;
		}
		set{
			this.id = value;
		}
	}
	
}
